/**
 * @Author: LiMeng
 * @Date: 2017/3/14
 */
var edit_role_func = function () {
    var $selected = $('#dg1').datagrid('getSelections');
    if ($selected.length !== 1) {
        layer.alert('请选择一个用户！');
        return false;
    }
    var $departmentTree = $('#department_tree');
    var $selectedTreeNode = $departmentTree.tree('getSelected');
    var deptId;
    //未选择时获取根节点
    if ($selectedTreeNode === null) {
        deptId = $departmentTree.tree('getRoot').id;
    }else {
        deptId = $selectedTreeNode.id;
    }
    var index = layer.open({
        type: 2,
        title: '授予用户角色',
        area: ['600px', '60%'],
        content: ROOT_PATH + '/permission/userRole/edit.shtml?id=' + $selected[0].id + "&deptId=" + deptId
    });
};
$(function () {
    /**
     * 根据部门编号初始化用户datagrid
     * @param deptCode
     */
    var initDataGrid = function (deptCode) {
        $('#dg1').datagrid({
            method: 'post',
            url: ROOT_PATH + '/permission/user/list.shtml',
            queryParams: {
                sort: 'addTime',
                order: 'desc',
                deptCode: deptCode
            },
            toolbar: '#tb1',
            //idField: 'id',
            fitColumns: true,
            pagination: true,
            rownumbers: true,
            sortOrder: true,
            striped: true,
            fit: true,
            columns: [[
                {field: 'id', width: 80, checkbox: true},
                {field: 'code', title: '用户编号', width: 80, sortable: true},
                {field: 'name', title: '用户名', width: 60, sortable: true},
                {field: 'realName', title: '姓名', width: 60, sortable: true},
                {
                    field: 'deptName',
                    title: '所属部门',
                    width: 100,
                    formatter: function (value, row, index) {
                        var name = '';
                        if (row.userDepartmentList && row.userDepartmentList.length > 0) {
                            for (var index in row.userDepartmentList) {
                                if (row.userDepartmentList[index].deptName != null) {
                                    name += ',' + row.userDepartmentList[index].deptName;
                                }
                            }
                            return name.substring(1);
                        } else {
                            return '';
                        }
                    }
                },
                {
                    field: 'roleNames',
                    title: '拥有角色',
                    width: 200,
                    formatter: function (value, row, index) {
                        var name = '';
                        if (row.userRoleList && row.userRoleList.length > 0) {
                            for (var index in row.userRoleList) {
                                if (row.userRoleList[index].roleName != null) {
                                    name += ',' + row.userRoleList[index].roleName;
                                }
                            }
                            return name.substring(1);
                        } else {
                            return '';
                        }
                    }
                }
            ]]
        });
    };
    //初始化组织结构树
    $('#department_tree').tree({
        url: ROOT_PATH + '/permission/department/getDepartmentTree.shtml?parentId=0',
        lines:true,
        onSelect: function (node) {
            //重新按部门加载datagrid
            $('#dg1').datagrid('load', {
                deptCode: node.attributes.code
            });
        },
        onLoadSuccess: function (node, data) {
            var deptId = data[0].id;
            var deptCode = data[0].attributes.code;
            //初始化加载datagrid
            initDataGrid(deptCode);
        }
    });
    $('#btn_clear').on("click", function () {
        $('#tb1').form('reset');
    });
    $('#btn_search').on("click", function () {
        $('#dg1').datagrid('load', $('#tb1').find(':input').serializeJson());
    });
    $('#btn_userRole_edit').on('click', function () {
        edit_role_func();
    });
});